<?php
namespace Admin\Controller;

class ProfessionController extends BaseController {


    public function __construct() {
        parent::__construct();
    }

    public function _empty(){
        $this->index();
    }

    public function index() {

        $Profession = D ('Profession');
        $professionList = $Profession->getAllProfessionByDid($this->admin['department_id']);

        $this->assign('professionList',$professionList);
        $this->display();
    }


    public function add() {

        if (!IS_POST) {
            $this->display();
        } else {

            $data['name'] = I ('request.name');
            $data['department_id'] = $this->admin['department_id'];
            $data['description'] = I ('request.description');

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Profession = D ('Profession');
            $flag = $Profession->_add($data);

            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        }

    }


    public function edit() {

        if (!IS_POST) {
            $id = I ('request.id');
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Profession = D ('Profession');
            $p = $Profession->getProfessionById($id) ;
            if (empty($p)) $this->error('专业不存在');

            $this->assign('profession',$p);
            $this->display();
        } else {

            $data['id'] = I ('request.id');
            $data['name'] = I ('request.name');
            $data['description'] = I ('request.description');

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Profession = D ('Profession');
            $flag = $Profession->_edit($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        }

    }


    public function del() {
        if (!IS_POST) $this->error('非法访问');

        $id = I ('request.id');
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $Profession = D ('Profession');
        $flag = $Profession->_del($id);

        if ($flag === '000000')
            $this->out('RESULT_OK','',__CONTROLLER__);
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
    }


}